package utils;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.StringTokenizer;
import java.util.ArrayList;
import java.math.*;

public class Formatter
{
  public static String longDate(Date d)
  {
      if(d == null) return "";
      SimpleDateFormat sdf = new SimpleDateFormat("MMM/dd/yy hh:mm");
      return sdf.format(d);
  }

  public static String shortDate(Date d)
  {
      if(d == null) return "";
	  SimpleDateFormat sdf = new SimpleDateFormat("MMM/dd/yy");
      String strConstructDate = sdf.format(d);
      return strConstructDate;	
  }
	
  public static String currency(String amount)
  {
	  float f;
	  if (amount == null || amount.equals(""))
	  {
		  f = 0.0f;
	  }
	  else
	  {
		  f = Float.parseFloat(amount);
	  }
	
	  return currency(f);
  }
  
  public static String currency(double amount) {
	

	NumberFormat f = DecimalFormat.getInstance();
	f.setMaximumFractionDigits(2);
	f.setMinimumFractionDigits(2);

    return "$" + f.format(new Double(amount));
  }

  public String pad(String string, int width) {
    if (string.length() >= width) {
      return string.substring(0, width);
    }

    StringBuffer output = new StringBuffer(string);
    for (int i=0; i < (width - string.length()); i++) {
      output.append(' ');
    }

    return output.toString();
  }

  //mm/dd/yyyy to yyyy-mm-dd
  public static String convertDate(String inputdate, String sourcetaken, String targettaken){
	  String outputdate = "";
	  if(inputdate!=null&&inputdate.indexOf(sourcetaken)!=-1){
		  outputdate = inputdate.substring(inputdate.lastIndexOf(sourcetaken)+1)
		             + targettaken + inputdate.substring(0,inputdate.indexOf(sourcetaken))
		             + targettaken + inputdate.substring(inputdate.indexOf(sourcetaken)+1,inputdate.lastIndexOf(sourcetaken));
	  }
	  return outputdate;
  }
  
  //yyyy-mm-dd to mm/dd/yyyy
  public static String convertDate2(String inputdate, String sourcetaken, String targettaken){
	  String outputdate = "";
	  if(inputdate!=null&&inputdate.indexOf(sourcetaken)!=-1){
		  outputdate = inputdate.substring(inputdate.indexOf(sourcetaken)+1,inputdate.lastIndexOf(sourcetaken))
		             + targettaken + inputdate.substring(inputdate.lastIndexOf(sourcetaken)+1)
		             + targettaken + inputdate.substring(0,inputdate.indexOf(sourcetaken));
	  }
	  return outputdate;
  }
  
//yyyy-mm-dd hh:mm:ss.0 to mm/dd/yyyy hh:mm AM|PM
  public static String convertDate3(String inputdate){
	  String outputdate = "";
	  if(inputdate!=null&&inputdate.indexOf("-")!=-1){
		  outputdate = inputdate.substring(inputdate.indexOf("-")+1,inputdate.lastIndexOf("-"))
		             + "/" + inputdate.substring(inputdate.lastIndexOf("-")+1)
		             + "/" + inputdate.substring(0,inputdate.indexOf("-"));
	  
	      String tempstr=inputdate.substring(inputdate.indexOf(" ")+1,inputdate.lastIndexOf(":"));
	      String tempstr2=inputdate.substring(inputdate.indexOf(":")+1,inputdate.lastIndexOf(":"));
	      String tempstr3="";
	      if(Integer.parseInt(tempstr2)<12)
		      tempstr3="AM";
	      else tempstr3="PM";
	          outputdate = outputdate + " " + tempstr + " " + tempstr3;
	  }
	  return outputdate;
  }
  
  public static String getNowDate()
  {
      Date objDate = new Date();
      SimpleDateFormat objSDateFormat= new SimpleDateFormat("MM/dd/yyyy");
      String strConstructDate = objSDateFormat.format(objDate);
      return strConstructDate;	
  }
  
  public static String getNowTime()
  {
      Date objDate = new Date();
      SimpleDateFormat objSDateFormat= new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
      String strConstructDate = objSDateFormat.format(objDate);
      return strConstructDate;	
  }
  
  public static String trimFormat(Date date,String pattern) throws IllegalArgumentException {
	  if(date==null)
	      throw new IllegalArgumentException("date");
	  if(pattern==null)
	      throw new IllegalArgumentException("format");
	  SimpleDateFormat objSDateFormat= new SimpleDateFormat(pattern);
	  return objSDateFormat.format(date);
  }
  
  public static ArrayList getStringTokens(String inputstr, String token){
	  ArrayList rtn = new ArrayList();
	  if(inputstr != null){
		  StringTokenizer st = new StringTokenizer(inputstr,token);
		  while (st.hasMoreTokens()){
			  rtn.add(st.nextElement());
		  }
	  }
	  return rtn;
  }
  
  public static String convertNull(String inputstr){
	  return inputstr == null?"":inputstr;
  }
  
  public static String twoNumberAdd(String strFirst, String strSecond) {
	BigDecimal objBigDecimalFirst = new BigDecimal(strFirst);
	BigDecimal objBigDecimalSecond = new BigDecimal(strSecond);
	return (objBigDecimalFirst.add(objBigDecimalSecond).toString());
  }
  
  public static String twoNumberMul(String strFirst, String strSecond) {
    BigDecimal objBigDecimalFirst = new BigDecimal(strFirst);
    BigDecimal objBigDecimalSecond = new BigDecimal(strSecond);
    return (objBigDecimalFirst.multiply(objBigDecimalSecond).toString());
  }
  
  public static String numChgStr2(String strNumber) {
	BigDecimal objBigDecimalInput = new BigDecimal(strNumber);
	BigDecimal objBigDecimalFive = new BigDecimal("0.005");
	BigDecimal objBigDecimalMove = objBigDecimalInput.add(objBigDecimalFive).
	        movePointRight(2);
	BigInteger objBigInteger = objBigDecimalMove.toBigInteger();
	BigInteger objBigIntegerHun = new BigInteger("100");
	BigInteger objBigIntegerResult[] = objBigInteger.divideAndRemainder(
	        objBigIntegerHun);
	return (objBigIntegerResult[0].toString() + "." +
	            objBigIntegerResult[1].toString());
  }
  
  public static String parseHtml(String strPara)
  {
     if(strPara==null||strPara.length()==0)
        return "";
     if(strPara.indexOf("<")==-1&&strPara.indexOf(">")==-1)
        return strPara;
     StringBuffer sBuf = new StringBuffer(strPara.length());
     char c;
     for(int i=0;i<strPara.length();i++)
      {
          c = strPara.charAt(i);
          if(c=='<')
             sBuf.append("&#60;");
          else if(c=='>')
                  sBuf.append("&#62;");
                else
                  sBuf.append(c);
      }
      return sBuf.toString();
  }
  
  public static void main(String args[]){
	  System.out.print(longDate(new Date()));
  }
}